package com.lanhao.aot.carrierintegration.lion360;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lanhao.aot.carrierintegration.AeIfRequestDTO;
import com.lanhao.aot.carrierintegration.Warehouse;

import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;

public class Lion360Util {
	
	private static final Logger logger = LoggerFactory.getLogger(Lion360Util.class);
	
//	public static final String APP_TOKEN = "dcfe81e2059c1f0e6e6263dbcb764885";
//	public static final String APP_KEY = "dcfe81e2059c1f0e6e6263dbcb7648850d0c1386bae3caf82229e7cf472d7b53";
//	public static final String ORDER_URL = "http://124.71.67.225:8080/toms/service";
	
	public static final String APP_TOKEN = "f24278fd34984b45c3a5596031a7eb85";
	public static final String APP_KEY = "f24278fd34984b45c3a5596031a7eb85f414aa2acaaacf5603cdcaa3b0bced7f";
	public static final String ORDER_URL = "http://track.360lion.com/api/service";
	
	public static final String MX = "MX";
	public static final String CL = "CL";
	public static final String MX_SHIPPING_METHOD = "MX1001";
	public static final String CL_SHIPPING_METHOD = "CL1017";
	
	public static void main(String[] args) {
		String countryCode = MX;
		AeIfRequestDTO request = new AeIfRequestDTO();
		request.setCountry_code(countryCode);
		request.setShipper_country_code(countryCode);
		Warehouse warehouse = new Warehouse();
		warehouse.setNationCode(countryCode);
		if(MX.equals(countryCode)) {
			request.setShipper_city("Chimalhuacán");
			request.setShipper_name("Luis Enrique Beltrán Tellez");
			request.setShipper_phone("5630818443");
			request.setWarehouse_code("CKMX000001");
			request.setShipper_post_code("563301");
			request.setShipper_province("Estado de México");
			request.setShipper_street("Calle cumbres manzana 52 lote 12 Ampliación San Agustín");
			
			warehouse.setCity("Centro Cuauhtemoc");
			warehouse.setPhone("5568981588");
			warehouse.setPostCode("06000");
			warehouse.setReceiver("MX-RTN-AE");
			warehouse.setState("Ciudad de México");
			warehouse.setStreet("Humboltd 47-102");
			warehouse.setWarehouseCode("CKMX000001");
		} else if(CL.equals(countryCode)) {
			
			
			// 请求纬狮物流结束，response={"agent_number":"60514036877126969793150","ask":"Success","label_type":"pdf","label_url":"https://file.360lion.com/getObsFile/lglabelcl01/20231012/764809505199357952_712696979315.pdf","message":"Success","messageEng":"Success","order_code":"WSHCL2853368041YQ","reference_no":"ac0837d48bb14fa1a6427e34e2ac676b","shipping_method_no":"712696979315","track_status":"1"}
			request.setShipper_city("Santiago");
			request.setShipper_name("KunKun");
			request.setShipper_phone("56953888888");
			request.setWarehouse_code("CKCL000001");
			request.setShipper_post_code("8361016");
			request.setShipper_province("Región Metropolitana");
			request.setShipper_street("Carmen 1969");
			
			warehouse.setCity("Santiago");
			warehouse.setPhone("56953888888");
			warehouse.setPostCode("8361016");
			warehouse.setReceiver("CL-RTN-AE");
			warehouse.setState("Región Metropolitana");
			warehouse.setStreet("Carmen 1967");
			warehouse.setWarehouseCode("CKCL000001");
		}
		
		createOrder(request, warehouse);
		
//		System.out.println("60514036877126969793150".substring(10));
	}
	
	public static void createOrder(AeIfRequestDTO afRequest, Warehouse warehouse) {
		JSONObject request = new JSONObject();
		request.put("appToken", APP_TOKEN);
		request.put("appKey", APP_KEY);
		request.put("service", "createOrder");

		JSONObject paramsJson = new JSONObject();
		paramsJson.put("reference_no", UUID.fastUUID().toString(true));
		String countryCode = afRequest.getShipper_country_code();
		String shippingMethod = StringUtils.EMPTY;
		if (MX.equals(countryCode)) {
			shippingMethod = MX_SHIPPING_METHOD;
		} else if (CL.equals(countryCode)) {
			shippingMethod = CL_SHIPPING_METHOD;
		}
		paramsJson.put("shipping_method", shippingMethod);
		paramsJson.put("country_code", warehouse.getNationCode());
		paramsJson.put("order_weight", 0.02);
		paramsJson.put("order_pieces", 1);
		paramsJson.put("is_return", 1);

		// 仓库信息
		JSONObject consignee = new JSONObject();
		consignee.put("consignee_street", warehouse.getStreet());
		consignee.put("consignee_street2", "");
		consignee.put("consignee_street3", "");
		consignee.put("consignee_name", warehouse.getReceiver());
		consignee.put("consignee_telephone", warehouse.getPhone());
		consignee.put("consignee_postcode", warehouse.getPostCode());
		consignee.put("consignee_province", warehouse.getState());
		consignee.put("consignee_city", warehouse.getCity());
		paramsJson.put("Consignee", consignee);

		// 发件人信息
		JSONObject shipper = new JSONObject();
		shipper.put("shipper_countrycode", afRequest.getShipper_country_code());
		shipper.put("shipper_province", afRequest.getShipper_province());
		shipper.put("shipper_city", afRequest.getShipper_city());
		shipper.put("shipper_street", afRequest.getShipper_street());
		shipper.put("shipper_postcode", afRequest.getShipper_post_code());
		shipper.put("shipper_name", afRequest.getShipper_name());
		shipper.put("shipper_telephone", afRequest.getShipper_phone());
		shipper.put("shipper_mobile", afRequest.getShipper_phone());
		paramsJson.put("Shipper", shipper);

		JSONArray itemArr = new JSONArray();
		JSONObject item = new JSONObject();
		item.put("invoice_cnname", "安和退货");
		item.put("invoice_enname", "AnHe Goods");
		item.put("invoice_quantity", 1);
		item.put("invoice_unitcharge", 0.01);
		item.put("invoice_weight", 0.02d);
		itemArr.add(item);
		paramsJson.put("ItemArr", itemArr);

		request.put("paramsJson", paramsJson);

		String requestString = request.toString();
		logger.info("开始请求纬狮物流获取标签，requestUrl={},request={}", ORDER_URL, requestString);
		String response = HttpRequest.post(ORDER_URL).body(requestString).timeout(60000).execute().body();
		logger.info("请求纬狮物流结束，response={}", response);
	}
}
